<template>
  <div>
    <licence-file-list
      ref="table"
      root-meta-url="/file/directory/enterprise-license"
      list-directory-url="/licence/enterprise/ls"
      :filter="filter"
      :mapper="mapper"
      search-key="licenceName"
      @upload-clicked="activeUploader"
      @mkdir-clicked="activeMkdir"
      @change-file-meta="activeChange"
      @change-dir-meta="activeDirChange"
    >
      <template
        slot="columns"
      >
        <el-table-column label="证照名称" prop="licenceName"></el-table-column>
        <el-table-column label="证号" prop="licenceNumber">
          <template slot-scope="{ row }">
            <span v-if="row.isDir && row.length"
              >具有 {{ row.length }} 个文件</span
            >
            <span v-else-if="row.licenceNumber">{{ row.licenceNumber }}</span>
          </template>
        </el-table-column>
        <el-table-column label="证书等级" prop="licenceLevel"></el-table-column>
        <el-table-column
          label="发证机关"
          prop="authorization"
        ></el-table-column>
        <time-gap-column
          width="200px"
          label="有效时间"
          start-key="validStartingTime"
          end-key="validCutOffTime"
        ></time-gap-column>
        <el-table-column label="续证准备时间">
          <template v-if="row['renewalPreparingTime']" slot-scope="{ row }">
            到期前<time-slot :value="row['renewalPreparingTime']"></time-slot>
          </template>
        </el-table-column>
      </template>
    </licence-file-list>
    <enterprise-licence-upload
      ref="uploader"
      @upload-success="freshTable"
    ></enterprise-licence-upload>
    <enterprise-licence-upload
      ref="changer"
      @upload-success="freshTable"
    ></enterprise-licence-upload>
    <make-dir ref="mkdir" @mkdir-success="freshTable"></make-dir>
    <dir-changer ref="dirChanger" @change-success="freshTable"></dir-changer>
  </div>
</template>

<script>
import LicenceFileList from "~/components/file-list/LicenceFileList";
import EnterpriseLicenceUpload from "~/components/file-list/licence-file-list/EnterpriseLicenceUpload";
import TimeGapColumn from "~/components/file-list/table-columns/TimeGapColumn";
import MakeDir from "~/components/file-list/MakeDir";
import DirChanger from "~/components/file-list/DirChanger";
import TimeSlot from "~/components/file-list/licence-file-list/TimeSlot";
export default {
  name: "EnterpriseLicenceFileList",
  components: {
    TimeSlot,
    DirChanger,
    MakeDir,
    EnterpriseLicenceUpload,
    LicenceFileList,
    TimeGapColumn,
  },
  data() {
    return {};
  },
  methods: {
    filter(v) {
      return Boolean(v && (v.licenceName || v.isDir));
    },
    mapper(v) {
      if (v.isDir) {
        v.licenceName = v.name;
      }
      return v;
    },
    activeUploader(dir) {
      this.$refs.uploader.$emit("open", dir);
    },
    freshTable(dir) {
      this.$refs.table.$emit("reload-dir", dir);
    },
    activeMkdir(dir) {
      this.$refs.mkdir.$emit("open", dir);
    },
    activeChange(dir, file) {
      this.$refs.changer.$emit("checkout", dir, file);
    },
    activeDirChange(currentDir, targetDir) {
      this.$refs.dirChanger.$emit("open", currentDir, targetDir);
    },
  },
};
</script>

<style scoped>
</style>
